﻿using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.UC.下单MTO;
using DXApplicationTangche.UC.下单MTO.Service;
using DXApplicationTangche.UC.门店下单.下单;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.订单一览新.销售确认单
{
    public partial class FormMTO销售一览 : DevExpress.XtraEditors.XtraForm
    {
        private List<ViewCustomerDailyAdjustDTO> viewCustomerDailyAdjustDTOs = new List<ViewCustomerDailyAdjustDTO>();
        private List<ViewCustomerDailyAdjustDTO> viewCustomerDailyAdjustList = new List<ViewCustomerDailyAdjustDTO>();
        public FormMTO销售一览()
        {
            InitializeComponent();
        }

        #region 事件
        private void gridView销售一览_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            String saleDate = FunctionHelper.GetbandedgridViewValue(this.bandedGridView销售一览, this.bandedGridView销售一览.FocusedRowHandle, "saleDate");
            String mobile = FunctionHelper.GetbandedgridViewValue(this.bandedGridView销售一览, this.bandedGridView销售一览.FocusedRowHandle, "mobile");
            Double finalPrice = 0.0;
            try
            {
                finalPrice = Convert.ToDouble(FunctionHelper.GetbandedgridViewValue(this.bandedGridView销售一览, this.bandedGridView销售一览.FocusedRowHandle, "finalPrice"));
            }
            catch { }
            //Double finalPrice = Convert.ToDouble(FunctionHelper.GetbandedgridViewValue(this.bandedGridView销售一览, this.bandedGridView销售一览.FocusedRowHandle, "finalPrice"));
            new FormMTO销售日记账(saleDate, mobile,finalPrice).ShowDialog();
            this.查询();
        }

        private void bandedGridView销售一览_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            try
            {
                if (e.Column.Name == "gridColumn是否包含未同步套号")
                {
                    if (Convert.ToBoolean(e.CellValue.ToString()))
                    {
                        e.DisplayText = "是";
                        e.Appearance.BackColor = Color.OrangeRed;
                    }
                    else
                    {
                        e.DisplayText = "否";
                        e.Appearance.BackColor = Color.LimeGreen;
                    }
                }
            }
            catch (Exception ex) { }
        }

        private void 补充定制订单信息ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            String saleDate = FunctionHelper.GetgridViewValue(this.gridView销售订单异常处置, this.gridView销售订单异常处置.FocusedRowHandle, "dateStamp");
            String mobile = FunctionHelper.GetgridViewValue(this.gridView销售订单异常处置, this.gridView销售订单异常处置.FocusedRowHandle, "mobile");
            CustomerPDTO customerPDTO = MTO订单Service.getCustomerPByMobile(mobile);
            new Form补充定制下单(
                saleDate,
                mobile,
                customerPDTO
                ).ShowDialog();
            this.bandedGridView销售一览.RefreshData();
            //刷新
            this.查询();
        }

        #endregion
        private void dateTimePicker开始时间_CloseUp(object sender, EventArgs e)
        {
            this.查询();
        }

        private void dateTimePicker结束时间_CloseUp(object sender, EventArgs e)
        {
            this.查询();
        }
        private void 查询()
        {
            this.viewCustomerDailyAdjustList = 
                new 销售一览Model(
                    this.dateTimePicker开始时间.Value.ToString("yyyy-MM-dd"),
                    this.dateTimePicker结束时间.Value.ToString("yyyy-MM-dd")
                    ).viewCustomerDailyAdjustDTOs;
            //销售一览刷新
            this.gridControl销售一览.DataSource = 
                new 销售一览Model(
                    this.dateTimePicker开始时间.Value.ToString("yyyy-MM-dd"), 
                    this.dateTimePicker结束时间.Value.ToString("yyyy-MM-dd")
                    ).viewCustomerDailyAdjustDTOs;
            this.bandedGridView销售一览.RefreshData();
            this.bandedGridView销售一览.ExpandAllGroups();

            //销售订单异常处置刷新
            this.gridControl销售订单异常处置.DataSource =
                销售确认Service.findViewCustomerDailyAdjustListBySaleDateBetween(
                    this.dateTimePicker开始时间.Value.ToString("yyyy-MM-dd"),
                    this.dateTimePicker结束时间.Value.ToString("yyyy-MM-dd")
                    ).salesOrderQCItemDTOS;
            this.gridView销售订单异常处置.RefreshData();
            this.gridView销售订单异常处置.ExpandAllGroups();

            //销售每日异常处置刷新
            List<SalesQCItemDTO> salesQCItemDTOS补充客户地址 = new List<SalesQCItemDTO>();
            List<SalesQCItemDTO> salesQCItemDTOS补充客户标签 = new List<SalesQCItemDTO>();

            List<SalesQCItemDTO> salesQCItemDTOS = 
                销售确认Service.findViewCustomerDailyAdjustListBySaleDateBetween(
                    this.dateTimePicker开始时间.Value.ToString("yyyy-MM-dd"),
                    this.dateTimePicker结束时间.Value.ToString("yyyy-MM-dd")
                    ).salesDailyQCItemDTOS;
            foreach(SalesQCItemDTO salesQCItemDTO in salesQCItemDTOS)
            {
                switch (salesQCItemDTO.defectCode) {
                    case "补充客户地址":
                        salesQCItemDTOS补充客户地址.Add(salesQCItemDTO);
                        break;
                    case "补充客户标签":
                        salesQCItemDTOS补充客户标签.Add(salesQCItemDTO);
                        break;
                }
            }

            this.gridControl补充客户地址.DataSource = salesQCItemDTOS补充客户地址;
            this.gridView补充客户地址.RefreshData();
            this.gridView补充客户地址.ExpandAllGroups();
            this.gridControl补充客户标签.DataSource = salesQCItemDTOS补充客户标签;
            this.gridView补充客户标签.RefreshData();
            this.gridView补充客户标签.ExpandAllGroups();

            this.gridControl销售每日异常处置.DataSource =
                销售确认Service.findViewCustomerDailyAdjustListBySaleDateBetween(
                    this.dateTimePicker开始时间.Value.ToString("yyyy-MM-dd"),
                    this.dateTimePicker结束时间.Value.ToString("yyyy-MM-dd")
                    ).salesDailyQCItemDTOS;
            this.gridView销售每日异常处置.RefreshData();
            this.gridView销售每日异常处置.ExpandAllGroups();
        }

        private void gridView销售订单异常处置_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.Name == "gridColumn状态")
            {
                if ("新建".Equals(e.CellValue.ToString()))
                {
                    e.Appearance.BackColor = Color.OrangeRed;
                }
                else if ("关闭".Equals(e.CellValue.ToString()))
                {
                    e.Appearance.BackColor = Color.Gray;
                }
            }
        }

        private void gridView销售订单异常处置_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.Name.Equals("gridColumn尺码"))
            {
                if (!String.IsNullOrWhiteSpace(e.DisplayText))
                {
                    try { e.DisplayText = e.DisplayText.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                }
            }
        }
        private void bandedGridView销售一览_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            try
            {
                if (e.Column.Name == "bandedGridColumn折扣")
                {
                    if (!"0.90".Equals(Math.Round(Convert.ToDecimal(e.DisplayText), 2).ToString())
                        && !"0.95".Equals(Math.Round(Convert.ToDecimal(e.DisplayText), 2).ToString()))
                    {
                        e.DisplayText = null;
                    }
                }
            }
            catch { }
        }

        private void gridView销售每日异常处置_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.Name == "gridColumn状态1")
            {
                if ("新建".Equals(e.CellValue.ToString()))
                {
                    e.Appearance.BackColor = Color.OrangeRed;
                }
                else if ("关闭".Equals(e.CellValue.ToString()))
                {
                    e.Appearance.BackColor = Color.Gray;
                }
            }
        }

        private void simpleButton成衣退货_Click(object sender, EventArgs e)
        {
            new Form现货退货().ShowDialog();
        }

        private void 补充客户地址ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String mobile = FunctionHelper.GetgridViewValue(this.gridView补充客户地址, this.gridView补充客户地址.FocusedRowHandle, "mobile");
            new FormMTO添加客户地址(mobile,"补充客户地址").ShowDialog();
        }

        private void gridView补充客户地址_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.Name == "gridColumn补充客户地址状态")
            {
                if ("新建".Equals(e.CellValue.ToString()))
                {
                    e.Appearance.BackColor = Color.OrangeRed;
                }
                else if ("关闭".Equals(e.CellValue.ToString()))
                {
                    e.Appearance.BackColor = Color.Gray;
                }
            }
        }

        private void gridView补充客户标签_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.Name == "gridColumn补充客户标签状态")
            {
                if ("新建".Equals(e.CellValue.ToString()))
                {
                    e.Appearance.BackColor = Color.OrangeRed;
                }
                else if ("关闭".Equals(e.CellValue.ToString()))
                {
                    e.Appearance.BackColor = Color.Gray;
                }
            }
        }

        private void 确认调价ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String mobile = FunctionHelper.GetbandedgridViewValue(this.bandedGridView每日整体调价, this.bandedGridView每日整体调价.FocusedRowHandle, "mobile");
            String dateStamp = FunctionHelper.GetbandedgridViewValue(this.bandedGridView每日整体调价, this.bandedGridView每日整体调价.FocusedRowHandle, "saleDate");
            销售确认Service.qcItemCloseByCustomerDaily(mobile, dateStamp, "调价确认");
        }

        private void dateTimePicker调价日期_CloseUp(object sender, EventArgs e)
        {
            this.viewCustomerDailyAdjustDTOs = 销售确认Service.getViewCustomerDailyAdjustByDateStamp(this.dateTimePicker调价日期.Value.ToString("yyyy-MM-dd"));
            this.gridControl每日整体调价.DataSource = this.viewCustomerDailyAdjustDTOs;
            this.bandedGridView每日整体调价.RefreshData();
            this.bandedGridView每日整体调价.ExpandAllGroups();
        }

        /// <summary>
        /// 每日调价整体确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton确认整体调价_Click(object sender, EventArgs e)
        {
            if (this.viewCustomerDailyAdjustDTOs.Count < 1)
            {
                MessageBox.Show("当日无调价信息");
                return;
            }
            else
            {
                销售确认Service.qcItemCloseByDateStamp(this.dateTimePicker调价日期.Value.ToString("yyyy-MM-dd"), "调价确认");
                MessageBox.Show("当日调价已确认");
                return;
            }
        }

        private void 补充客户标签ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String mobile = FunctionHelper.GetgridViewValue(this.gridView补充客户标签, this.gridView补充客户标签.FocusedRowHandle, "mobile");
            new Form补充客户标签(mobile).ShowDialog();
            this.查询();
        }

        private void 查看销售订单文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog folder = new System.Windows.Forms.FolderBrowserDialog();
            String fileName = 
                FunctionHelper.GetbandedgridViewValue(this.bandedGridView销售一览, this.bandedGridView销售一览.FocusedRowHandle, "mobile")
                +"_"
                +FunctionHelper.GetbandedgridViewValue(this.bandedGridView销售一览, this.bandedGridView销售一览.FocusedRowHandle, "saleDate");

            if (folder.ShowDialog() == DialogResult.OK)
            {
                //SelectedPath:获取文件夹绝对路径,显示在textbox里面
                this.createSaleBillFile(fileName, folder.SelectedPath);
                MessageBox.Show("操作完成");
            }

            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                
            }
            catch (Exception)
            {
                MessageBox.Show("操作失败");
            }
        }

        private void createSaleBillFile(String fileName,String selectPath) {
            foreach (ViewCustomerDailyAdjustDTO item in this.viewCustomerDailyAdjustList)
            {
                if (fileName.Equals(item.mobile + "_" + item.saleDate))
                {
                    //  返回程序本地路径
                    String path = 销售确认Service.findSaleOrderFileById(item);
                    if (!String.IsNullOrEmpty(path))
                    {
                        try
                        {
                            String savePath = selectPath + "\\";
                            File.Move(path, path.Replace(@"pdf\", savePath));
                        }
                        catch (Exception)
                        {

                        }
                    }
                    break;
                }
            }
        }
        private void createSaleBillFile(String selectPath)
        {
            foreach (ViewCustomerDailyAdjustDTO item in this.viewCustomerDailyAdjustList)
            {
                //  返回程序本地路径
                String path = 销售确认Service.findSaleOrderFileById(item);
                if (!String.IsNullOrEmpty(path))
                {
                    try
                    {
                        String savePath = selectPath + "\\";
                        File.Move(path, path.Replace(@"pdf\", savePath));
                    }
                    catch (Exception)
                    {

                    }
                }
            }
        }

        private void simpleButton打印当日销售订单_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog folder = new System.Windows.Forms.FolderBrowserDialog();
            
            if (folder.ShowDialog() == DialogResult.OK)
            {
                //SelectedPath:获取文件夹绝对路径,显示在textbox里面
                this.createSaleBillFile(folder.SelectedPath);
                MessageBox.Show("操作完成");
            }

            try
            {
                OpenFileDialog ofd = new OpenFileDialog();

            }
            catch (Exception)
            {
                MessageBox.Show("操作失败");
            }
        }
    }

    public class 销售一览Model
    {
        public List<ViewCustomerDailyAdjustDTO> viewCustomerDailyAdjustDTOs = new List<ViewCustomerDailyAdjustDTO>();
        public 销售一览Model(String dateTimeStr, String dateTimeEnd)
        {
            //  查询
            this.viewCustomerDailyAdjustDTOs = 销售确认Service.findViewCustomerDailyAdjustListBySaleDateBetween(dateTimeStr, dateTimeEnd).viewCustomerDailyAdjustDTOS;
            foreach (ViewCustomerDailyAdjustDTO item in viewCustomerDailyAdjustDTOs)
            {
                //  行内计算
                item.build();
            }
        }
    }
}
